<?php /** @noinspection DuplicatedCode */
/**
 * 导出三星活动数据
 * 每天上午10点发送头一天的数据
 */

ini_set('display_errors', 1);
ini_set('memory_limit', -1); //不限制内存
ini_set("error_reporting", E_ALL);//显示所有错误
ob_start();
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\SamsungActivity\SamsungJoinUserListModel;
use Biz\service\SamsungActivity\SamsungJoinUserListService;
use Biz\tools\Mail;
use Doraemon\tools\Log;


const EMAIL_MAP = [
    'renzhifan@haohaozhu.com',
    'siliuchuo@haohaozhu.com',
    'zhangfenglin@haohaozhu.com'
];

try {
    $startTime = $argv[1] ?? date("Y-m-d", strtotime("-1 day", time()));
    $endTime = $argv[2] ?? date('Y-m-d H:i:s', strtotime($startTime) + 86399);

    $pageTitle = '三星活动数据导出'.date('Y-m-d H-i-s').'提交记录';

    $where = [
        'addtime[>=]' => $startTime,
        'addtime[<=]' => $endTime,
        'status' => SamsungJoinUserListModel::STATUS_NORMAL,
    ];
    $list = SamsungJoinUserListModel::selectByWhere($where);
    SamsungJoinUserListService::exportList($list, SamsungJoinUserListService::SAVE_TYPE_FILE, $pageTitle);
    $filePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.$pageTitle.'.xlsx';
    echo $filePath.PHP_EOL;
    Mail::send(EMAIL_MAP, $pageTitle, $pageTitle.'</br>见附件'."</br>数据范围：$startTime | $endTime ", $filePath);
    echo '发送邮件成功'.PHP_EOL;
} catch (Exception $exception) {
    Log::error('ExportSamsungActivity.php', $exception);
}
